package com.example.smart.base.ui.activity

import android.Manifest
import androidx.annotation.CallSuper
import androidx.camera.view.PreviewView
import androidx.viewbinding.ViewBinding
import com.google.zxing.Result
import com.king.camera.scan.BaseCameraScan
import com.king.camera.scan.CameraScan
import com.king.camera.scan.analyze.Analyzer
import com.king.camera.scan.util.PermissionUtils
import com.king.zxing.DecodeConfig
import com.king.zxing.analyze.MultiFormatAnalyzer
import com.example.smart.base.dialog.TipsDialog
import com.example.lib.utils.DeviceUtils
import com.example.lib.utils.permission.RequestPermission
import com.example.lib.utils.statusbar.StatusBarUtils

abstract class BaseCameraActivity<V : ViewBinding> : BaseActivity<V>(), CameraScan.OnScanResultCallback<Result> {
    private var isToSetting = false
    private lateinit var mCameraScan: CameraScan<Result>

    private val openPermissionDialog by lazy {
        com.example.smart.base.dialog.TipsDialog(this@BaseCameraActivity).also {
            it.setMsg(getString(com.example.lib.R.string.please_camera_permission))
            it.setCancelText(getString(com.example.lib.R.string.exit))
            it.setOkText(getString(com.example.lib.R.string.go_setting))
            it.setCancelListener { dialog ->
                dialog.dismiss()
                finish()
            }
            it.setDoneListener {
                DeviceUtils.toSetting(this@BaseCameraActivity)
            }
        }
    }

    @CallSuper
    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@BaseCameraActivity,
            statusBarColor = com.example.lib.R.color.translate)
        mCameraScan = createCameraScan(getPreviewView())
        initCameraScan(mCameraScan)
        if (isAutoStartCamera()) startCamera()
    }

    override fun onResume() {
        super.onResume()
        if (isToSetting && RequestPermission.check(this@BaseCameraActivity, Manifest.permission.CAMERA)) {
            openPermissionDialog.dismiss()
            startCamera()
            isToSetting = false
        }
    }

    protected abstract fun getPreviewView(): PreviewView

    private fun createCameraScan(previewView: PreviewView): CameraScan<Result> {
        return BaseCameraScan(this, previewView)
    }

    private fun initCameraScan(cameraScan: CameraScan<Result>) {
        cameraScan.setAnalyzer(createAnalyzer()).setOnScanResultCallback(this)
    }

    private fun createAnalyzer(): Analyzer<Result> {
        val decodeConfig = DecodeConfig().apply {
            isSupportVerticalCode = true
            isSupportLuminanceInvert = true
        }
        return MultiFormatAnalyzer(decodeConfig)
    }

    protected fun startCamera() {
        if (this::mCameraScan.isInitialized) {
            if (PermissionUtils.checkPermission(this, Manifest.permission.CAMERA)) {
                getPreviewView().postDelayed({
                    mCameraScan.startCamera()
                }, 300)
            } else {
                RequestPermission.requestCamera(this@BaseCameraActivity) {
                    if (it) {
                        startCamera()
                    } else {
                        isToSetting = true
                        openPermissionDialog.show()
                    }
                }
            }
        }
    }

    /**
     * 释放相机
     */
    private fun releaseCamera() {
        if (this::mCameraScan.isInitialized) {
            mCameraScan.release()
        }
    }

    protected fun stopScan() {
        if (this::mCameraScan.isInitialized) {
            mCameraScan.setAnalyzeImage(false)
        }
    }

    protected fun startScan() {
        if (this::mCameraScan.isInitialized) {
            mCameraScan.setAnalyzeImage(true)
        }
    }

    open fun isAutoStartCamera() = true

    override fun onDestroy() {
        releaseCamera()
        super.onDestroy()
    }
}